

summ = open_dataset('data/final_long') %>%
  filter(in_fec == 1) %>%
  mutate(r_val = ifelse(r_pcc + r_sp + r_jfc > 0, r_pcc + r_sp + r_jfc, 0),
         d_val = ifelse(d_pcc + d_sp + d_jfc > 0, d_pcc + d_sp + d_jfc, 0)) %>%
  filter(r_val > 0 | d_val > 0)%>%
  select(cycle, in_cl, in_l2, r_val, d_val)%>%
  collect()

setDT(summ)

levs = c(0.001, 50, 100, 200, 500, 1000, Inf)
lbls = c("(0,50)", "[50,100)", "[100,200)","[200,500)","[500,1000)","[1000,max)")

ds = summ[
  d_val >= 0.001,
  .(cl = mean(in_cl), l2 = mean(in_l2), cl_l2 = mean(in_cl & in_l2)),
  .(cycle, cont_bins = cut(d_val, breaks = levs, right = F, labels = lbls))
]

rs = summ[
  r_val >= 0.001,
  .(cl = mean(in_cl), l2 = mean(in_l2), cl_l2 = mean(in_cl & in_l2)),
  .(cycle, cont_bins = cut(r_val, breaks = levs, right = F, labels = lbls))
]


ds = ds[order(cont_bins)]
rs = rs[order(cont_bins)]

save(ds, rs, file = 'summary_data/tableA4_A5.rda')
